package main

import (
	"fmt"

	_ "github.com/go-sql-driver/mysql"

	"github.com/jinzhu/gorm"
)

// 创建animal结构体
	type Animal struct {
		ID   int64  `gorm:"column:name;not null;primary_key;AUTO_INCREMENT"`
		Name string `gorm:"column:name"`
		Age  int64  `gorm:"column:age"`
	}

// 定义表名，animals,不定义则按结构体名小写复数形式
func (Animal) TableName() string {
	return "animals"
}

func main() {
	// 连接数据库
	db, err := gorm.Open("mysql", "root:root@/gormdemo?charset=utf8&parseTime=True&loc=Local")
	if err != nil {
		// 报错：连接数据库失败
		fmt.Printf("连接数据库失败，错误信息：%v\n", err)
	}

	defer db.Close()

	// 初始化新增行记录的结构体
	// animal := Animal{Name: "张三", Age: 18}
	// db.Create(animal)

	//批量插入
	animals := []Animal{
		{Name:"李四",Age:18},
		{Name:"王五",Age:18},
		{Name:"赵六",Age:18},
	}

	for _,value := range animals{
		db.Create(value)
	}
}
